const lib = require('./lib')
const lodash = require('lodash')
const rq = require('inquirer')

const commandOptions = [
    //{ com:"", des:"", action:""}
    { com: "-v", des: "版本", action: "console.log(require(\"./package.json\").version)" },
    { com: "publish", des: "获取 publish.xml", action: "console.table(lib.allPublishPlugins())" },
    { com: "jsplugins", des: "获取 jsplugins.xml", action: "console.log(\"在线模式:\");console.table(lib.getJSpluginLocal().online);console.log(\"离线模式:\");console.table(lib.getJSpluginLocal().offline)" },
    { com: "enable", des: "关闭/启用 JS 加载项", action: "require('./enable.js')" }
]

function commandDes(str1, str2) {
    let len = 25;
    let la = str1.length;
    if (la >= len) {
        throw console.error("字符超出");
    }
    return `${str1}${" ".repeat(len - la)}${str2}`
}

const des = lodash.map(commandOptions, o => commandDes(o.com, o.des))

rq.prompt({
    type: 'list',
    choices: des,
    name: 'choice',
    message: "WPS JS加载项管理CLI\n\n请通过↑、↓键选择对应的项："
}).then(r => {
    let foo = r.choice;
    let bar = foo.match(/^.{24}/)[0].replace(/\ /g, "")
    let line = lodash.find(commandOptions, { "com": bar })
    let action = line.action;
    eval(action)
}).catch(e => {
    console.log(e)
})